/**
 * Created by observer on 15/10/19.
 */

var tab = function(options){
    return tab.init(options);
};

tab.init = function(options){

    var opts = $.extend({
        tabBtn: ".witkey-list-item",
        wrap: ".index-witkey-list",
        idName: "witkey-catalog-id",
        url: "http://rap.zbjwork.com/mockjsdata/54/index/ajaxshop?id=",
        tmpl: require("city:widget/index/witkey/tmpl/tpl.witkey-list.tmpl")
    },options);

    var self = this;

    var tabBtn = $(opts.tabBtn),
        wrap = $(opts.wrap+" ul");

    tabBtn.on("click",function(){


        var dataId = $(this).attr(opts.idName);

        console.log(opts.idName);

        var renderDom = wrap.addClass("text-nothing").filter(function(index){
            return $(this).attr(opts.idName) == dataId;
        }).removeClass("text-nothing");

        if(!renderDom.children().length){
            $.ajax({
                type: "get",
                dataType: "json",
                url: opts.url + dataId,
                success: function(json){
                    var msgTpl;

                    if($.isArray(json)){
                        msgTpl = opts.tmpl(json[0]);
                    }else{
                        msgTpl = opts.tmpl(json);
                    }

                    setTimeout(function(){
                        renderDom.removeClass("text-loading").html(msgTpl);
                    },500)

                }

            });
        }

    });

};

module.exports = tab;

